﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using OnlineManagement.DBUtility;

namespace OnlineManagement.SQLServerDAL
{
    /// <summary>
    /// 乳猪断奶登记,数据访问层
    /// </summary>
    public class WeanedRecord:OnlineManagement.IDAL.IWeanedRecord
    {
        public WeanedRecord() { }

        /// <summary>
        /// 添加 乳猪断奶登记
        /// </summary>
        /// <param name="record"></param>
        /// <param name="returnValue"></param>
        /// <param name="isChangeSowPigPen">是否改变母猪猪舍，0-不改变，1-改变</param>
        /// <param name="changedToPigPenId">改变后的猪舍号</param>
        /// <returns></returns>
        public bool AddWeanedRecord(Model.WeanedRecord record,bool isChangeSowPigPen,Guid changedToPigPenId, out string returnValue) {
            returnValue = string.Empty;
            if(record==null){
                returnValue = "数据不能为空";
                return false;
            }
            if(record.SowID.Equals(Guid.Empty)){
                returnValue = "数据不能为空";
                return false;
            }
            string proceName = Common.PubProcedures.SowWeanedRecordAdd;
            SqlParameter[] parameters = 
            {
                 new SqlParameter("@HoggeryID",SqlDbType.Int),
                 new SqlParameter("@SowID",SqlDbType.UniqueIdentifier),
                 new SqlParameter("@HybrizationRecord",SqlDbType.UniqueIdentifier),
                 new SqlParameter("@PropagateRecordID",SqlDbType.UniqueIdentifier),
                 new SqlParameter("@WeanedDate",SqlDbType.Date),
                 new SqlParameter("@WeanedChildCount",SqlDbType.TinyInt),
                 new SqlParameter("@isChangeSowPigPen",SqlDbType.Bit),
                 new SqlParameter("@ChangeToPigPenID",SqlDbType.UniqueIdentifier),
                 new SqlParameter("@SurvivalRate",SqlDbType.NVarChar,10),
                 new SqlParameter("@Comment",SqlDbType.NVarChar,2000),

                 new SqlParameter("@rowsAffected",SqlDbType.TinyInt,1),
                 new SqlParameter("@msg",SqlDbType.NVarChar,1000)
            };
            parameters[0].Value = record.HoggeryID;
            parameters[1].Value = record.SowID;
            parameters[2].Value = record.HybrizationRecord;
            parameters[3].Value = record.PropagateRecordID;
            parameters[4].Value = record.WeanedDate;
            parameters[5].Value = record.WeanedChildCount;
            parameters[6].Value = isChangeSowPigPen;
            parameters[7].Value = changedToPigPenId;
            parameters[8].Value = record.SurvivalRate;
            parameters[9].Value = record.Comment;

            parameters[10].Direction = ParameterDirection.Output;
            parameters[11].Direction = ParameterDirection.Output;
            try
            {
                int rowsAffected;
                DbHelperSQL.RunProcedure(proceName, parameters, out rowsAffected, out returnValue);
                return rowsAffected > 0 ? true : false;
            }catch(Exception ex){
                throw ex;
            }
        }
    }
}
